package com.intro.action;

import java.util.Date;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;

import com.intro.manager.INewsManager;
import com.intro.manager.impl.NewsManagerImpl;
import com.intro.util.JsonDateValueProcessor;
import com.intro.util.Page;
import com.opensymphony.xwork2.ActionSupport;



public class AjGetNewsAction extends ActionSupport {

	private int currentPage;
	private Page page;
	private INewsManager newsManager = new NewsManagerImpl();
	
	
	public String execute() throws Exception {
		
		Page page = newsManager.findAllNews(currentPage, 10);
		JSONObject jobj = null;
		try {
			ServletActionContext.getResponse().setContentType("html/txt");
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			JsonConfig jsonConfig = new JsonConfig(); 
		    jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor("dd-MM-yyyy"));
			jobj = net.sf.json.JSONObject.fromObject(page,jsonConfig);
			ServletActionContext.getResponse().getWriter().write(jobj.toString());
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(jobj.toString());
		//把page放入请求域，在把他转换冲 json格式 在使用tmpl
		//ServletActionContext.getRequest().setAttribute("page", page);
		System.out.println(currentPage);
/*		Map map = new HashMap();
		map.put("page", currentPage);
		map.put("total", "10");
		JSONObject object = new JSONObject(map);
		System.out.print(object.toString());
//		response.getWriter().write("<li style='opacity:0;-moz-opacity: 0;filter: alpha(opacity=0);'><p> Morbi eget tristique dui. Vivamus nec turpis eu nisi euismod accumsan sed in tortor. Maecenas laoreet leo ut tortor viverra facilisis.</p></li>");
		ServletActionContext.getResponse().getWriter().write(object.toString());
		ServletActionContext.getResponse().getWriter().flush();
		ServletActionContext.getResponse().getWriter().close();*/
		return "success";
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}


	
}
